<template>
  <div class="white-box">
    <el-tabs v-model="tabsActive" type="card" >
      <template v-for="(item, index) in tabs">
        <el-tab-pane :label="item.label" :name="item.value"></el-tab-pane>
      </template>
    </el-tabs>

    <el-table class="table-mb" v-loading="loading" :data="list" :header-cell-style="{ background: '#F5F7FA' }"
              element-loading-background="rgba(255, 255, 255, 0.6)" row-key="sid">
      <el-table-column prop="configName" label="类型"></el-table-column>
<!--      <el-table-column prop="configKey" label="参数名称"></el-table-column>-->
<!--      <el-table-column prop="configValue" label="值" :show-overflow-tooltip="true"></el-table-column>-->
      <el-table-column align="center" prop="configValue" label="值"  min-width="200">
        <template #default="{ row }">
          <div v-if="row.configKey === 'fen_yong_type'">
            <el-switch
                v-model="row.configValue"
                active-text="按金额"
                inactive-text="按比例"
                :loading="switchLoading"
                :before-change="beforeSwitchChange"
                active-value="按金额"
                inactive-value="按比例"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #409eff"
            />
          </div>
          <div v-else>
            {{row.configValue}}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="note" label="备注"></el-table-column>
      <el-table-column align="center" prop="status" label="状态"  width="100">
        <template #default="{ row }">
          <el-text v-if="row.status === 0" type="success">正常</el-text>
          <el-text v-else-if="row.status === 1" type="info">停用</el-text>
          <el-text v-else type="info"></el-text>
        </template>
      </el-table-column>
      <el-table-column prop="created" label="创建时间" width="160"> </el-table-column>
      <el-table-column label="操作" width="180" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleEdit(row,{sid:row.sid})">编辑</el-button>
<!--          <el-popconfirm :persistent="false" title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消"-->
<!--                         @confirm="handleDelete(row.sid)">-->
<!--            <template #reference>-->
<!--              <el-button link type="danger">删除</el-button>-->
<!--            </template>-->
<!--          </el-popconfirm>-->
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogShow" :title="dialogParams.type == 'add'?'添加':'编辑'" width="660px" :append-to-body="true" :destroy-on-close="true" :close-on-click-modal="false" @close="closeDialog">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules" label-width="120px" style="width: 560px">
<!--        <el-form-item label="类型" prop="configKey">-->
<!--          <el-input v-model.trim="dialogForm.configKey" clearable placeholder="请输入参数键名" disabled/>-->
<!--        </el-form-item>-->
        <el-form-item label="类型" prop="configName">
          <el-text tag="b">{{dialogForm.configName}}</el-text>
<!--          <el-input v-model.trim="dialogForm.configName" clearable placeholder="请输入参数名称" disabled/>-->
        </el-form-item>
        <el-form-item label="值" prop="configValue" v-if="dialogForm.configKey == 'fen_yong_type'">
          <el-radio-group v-model="dialogForm.configValue">
            <el-radio value="按比例">按比例</el-radio>
            <el-radio value="按金额">按金额</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="值" prop="configValue" v-else>
          <el-input v-model.trim="dialogForm.configValue" clearable placeholder="请输入参数键值"/>
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model.trim="dialogForm.note" clearable placeholder="请输入备注"/>
        </el-form-item>
<!--        <el-form-item label="删除" prop="deletable">-->
<!--          <el-radio-group v-model="dialogForm.deletable">-->
<!--            <el-radio :value="0">可以</el-radio>-->
<!--            <el-radio :value="1">不可以</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="dialogForm.status">
            <el-radio :value="0">正常</el-radio>
            <el-radio :value="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="dialogParams.loading" @click="submitDialogForm(dialogFormRef,false,getDetail)">
              确认
            </el-button>
<!--            <el-button @click="resetDialogForm(dialogFormRef)">重置</el-button>-->
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>

    <go-back/>
  </div>
</template>

<script setup>
import {
  getCurrentInstance,
  ref,
  reactive,
  onMounted,
  watch,
  computed
} from "vue"
import listMixins from "@mixins/hook/list"
import {
  useRouter
} from "vue-router"
import {
  CirclePlus, Search
} from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"

const router = useRouter()
const {
  proxy
} = getCurrentInstance()

/**
 * minxin列表加载
 */
const {
  page,
  pageSizeNum,
  pageSize,
  loading,
  totalNums,
  searchParams,
  searchBtnLoading,
  apiPath,
  list,
  dialogFormRef,
  dialogShow,
  dialogForm,
  dialogParams,
  handleSearch,
  getData,
  handleAdd,
  handleEdit,
  resetDialogForm,
  handleAllSelected,
  submitDialogForm,
  closeDialog,
  handleAction,
  handleDelete
} = listMixins()

dialogForm.deletable = 0;//是否可删除：0=可以删除，1=不可以删除，默认0
dialogForm.configKey = "";
dialogForm.configName = "";
dialogForm.configValue = "";
dialogForm.note = "";
dialogForm.status = 0;

apiPath.value = '/configs';

const tabs = [
  {label:'营销配置',value:1},
  {label:'提现配置',value:2},
];
const tabsActive = ref(1)
watch(tabsActive,newValue => {
  getDetail()
});
const getDetail = async () =>{
  loading.value = true
  await proxy.$axios
      .get(`/configs/commission/${tabsActive.value}`)
      .then((res) => {
        if (res.data.code == 200) {
          let result = res.data.data
          if(result){
            list.value = result
          }
        } else {
          proxy.$message({message: res.data.msg, type: "error"})
        }
      }).catch((err) => {

      }).finally(()=>{
        loading.value = false
      })
}

onMounted(() => {
  // getData()
  getDetail();
})
const dialogFormRules = reactive({
  configKey: [{
    required: true,
    message: "请输入参数键名",
    trigger: "blur"
  }],
  configName: [{
    required: true,
    message: "请输入参数名称",
    trigger: "blur"
  }],
  configValue: [{
    required: true,
    message: "请输入参数键值",
    trigger: "blur"
  }]
})
const switchLoading = ref(false);
const beforeSwitchChange = () =>{
  let item = list.value.find(item => item.configKey === 'fen_yong_type');
  let data = {
    configValue:item.configValue == '按金额' ?'按比例':'按金额'
  }
  switchLoading.value = true
  return new Promise(async (resolve, reject) => {
    let url = apiPath.value;
    url += `/${item.sid}`
    await proxy.$axios.put(url, data)
        .then((res) => {
          switchLoading.value = false
          if (res.data.code == 200) {
            proxy.$message({
              message: `修改成功`,
              type: "success"
            })
            return resolve(true)
          } else {
            proxy.$message({
              message: res.data.msg,
              type: "error"
            })
            return reject(new Error('Error'))
          }
        })
        .catch((err) => {
          switchLoading.value = false
          return reject(new Error('Error'))
        })
  })

}

</script>

<style lang="less">

</style>